<?php

/**
 * 库存控制器
 */

namespace app\admin\controller;

use think\Request;
use app\admin\service\InventoryCount as InventoryCountService;
use app\admin\validate\InventoryCount as InventoryCountValidate;

class InventoryCount extends Base
{

    protected $request;
    protected $service;
    protected $validate;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new InventoryCountService();
        $this->validate = new InventoryCountValidate();
    }

    /**
     * @describe 获取库存统计列表
     * @author: Bobo
     * @date: 2025-08-21 10:45:09
     * @param [type] @required
     * @return Renderable
     */
    public function getInventoryCountList()
    {
        $param = $this->request->param();

        $pageIndex = $param['pageIndex']  ?? 1;
        $pageSize = $param['pageSize'] ?? 20;
        if ($pageIndex < 1) {
            $pageIndex = 1;
        }
        if ($pageSize < 1) {
            $pageSize = 20;
        }
        if (isset($param['product_id']) && $param['product_id']) {
            $where[] = ['productid', 'in', $param['product_id']];
        }
        if (isset($param['warehouse_id']) && $param['warehouse_id']) {
            $where[] = ['wid', 'in', explode(',', $param['warehouse_id'])];
        }
        if (isset($param['is_futures']) && $param['is_futures'] > -1) {
            $where[] = ['is_futures', '=', $param['is_futures']];
        }
        if (isset($param['arrival_date_start']) && $param['arrival_date_start']) {
            $where[] = ['arrival_date', '>=', $param['arrival_date_start']];
        }
        if (isset($param['arrival_date_end']) && $param['arrival_date_end']) {
            $where[] = ['arrival_date', '<=', $param['arrival_date_end']];
        }

        // 排序参数
        $total_qty = $param['total_qty'] ?? '';
        $available_shipment_qty = $param['available_shipment_qty'] ?? '';
        $shipped_qty = $param['shipped_qty'] ?? '';
        $lock_qty = $param['lock_qty'] ?? '';
        $actually_available_qty = $param['actually_available_qty'] ?? '';

        $res = $this->service->getInventoryCountList($where ?? [], $pageIndex, $pageSize, $total_qty, $available_shipment_qty, $shipped_qty, $lock_qty, $actually_available_qty);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 获取已发货订单列表
     * @author: Bobo
     * @date: 2025-08-21 10:45:09
     * @param [type] @required
     * @return Renderable
     */
    public function getShippedOrderList()
    {
        $param = $this->request->param();
        if (!$this->validate->scene('getShippedOrderList')->check($param)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $pageIndex = $param['pageIndex']  ?? 1;
        $pageSize = $param['pageSize'] ?? 20;
        if ($pageIndex < 1) {
            $pageIndex = 1;
        }
        if ($pageSize < 1) {
            $pageSize = 20;
        }
        if (isset($param['product_id']) && $param['product_id']) {
            $where[] = ['product_id', '=', $param['product_id']];
        }
        if (isset($param['warehouse_id']) && $param['warehouse_id']) {
            $where[] = ['warehouse_id', 'in', explode(',', $param['warehouse_id'])];
        }

        $res = $this->service->getShippedOrderList($where ?? [], $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 获取锁定订单列表
     * @author: Bobo
     * @date: 2025-08-21 10:45:09
     * @param [type] @required
     * @return Renderable
     */
    public function getLockOrderList()
    {
        $param = $this->request->param();
        if (!$this->validate->scene('getLockOrderList')->check($param)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $pageIndex = $param['pageIndex']  ?? 1;
        $pageSize = $param['pageSize'] ?? 20;
        if ($pageIndex < 1) {
            $pageIndex = 1;
        }
        if ($pageSize < 1) {
            $pageSize = 20;
        }
        if (isset($param['product_id']) && $param['product_id']) {
            $where[] = ['product_id', '=', $param['product_id']];
        }
        if (isset($param['warehouse_id']) && $param['warehouse_id']) {
            $where[] = ['warehouse_id', 'in', explode(',', $param['warehouse_id'])];
        }
        if (isset($param['is_futures']) && $param['is_futures'] > -1) {
            $where[] = ['is_futures', '=', $param['is_futures']];
        }
        if (isset($param['arrival_date']) && $param['arrival_date']) {
            $where[] = ['arrival_date', '=', $param['arrival_date']];
        }


        $res = $this->service->getLockOrderList($where ?? [], $pageIndex, $pageSize);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 统计库存总数变更
     * @author: jiang
     * @date: 2025-01-27 10:00:00
     * @param [type] @required
     * @return Renderable
     */
    public function getInventoryTotalCount()
    {
        $param = $this->request->param();

        // 参数验证
        if (!$this->validate->scene('getInventoryTotalCount')->check($param)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $res = $this->service->getInventoryTotalCount($param);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
